package com.ecommerce.product.controller;

import com.ecommerce.common.core.ApiResult;
import com.ecommerce.common.core.PageResult;
import com.ecommerce.product.dto.ProductInfo;
import com.ecommerce.product.dto.ProductQueryRequest;
import com.ecommerce.product.entity.Product;
import com.ecommerce.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品管理控制器
 * 
 * @author ecommerce
 * @since 1.0.0
 */
@RestController
@RequestMapping("/api/v1/products")
@RequiredArgsConstructor
@Tag(name = "商品管理", description = "商品相关的API接口")
public class ProductController {

    private final ProductService productService;

    /**
     * 根据ID列表批量获取商品信息
     */
    @GetMapping("/batch")
    @Operation(summary = "批量获取商品信息", description = "根据商品ID列表批量获取商品信息")
    @ApiResponse(responseCode = "200", description = "获取成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ProductInfo.class)))
    public ApiResult<List<ProductInfo>> getProductsByIds(
            @Parameter(description = "商品ID列表", required = true)
            @RequestParam("productIds") List<Long> productIds) {
        List<ProductInfo> products = productService.getProductsByIds(productIds);
        return ApiResult.success(products);
    }

    /**
     * 获取商品列表（分页）
     */
    @GetMapping
    @Operation(summary = "获取商品列表", description = "分页获取商品列表")
    @ApiResponse(responseCode = "200", description = "获取成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Product.class)))
    public ApiResult<PageResult<Product>> getProducts(
            @Parameter(description = "商品名称关键字")
            @RequestParam(required = false) String keyword,
            @Parameter(description = "商品状态")
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        ProductQueryRequest request = new ProductQueryRequest();
        request.setKeyword(keyword);
        request.setStatus(status);
        request.setPage(page);
        request.setSize(size);
        return productService.pageProducts(request);
    }

    /**
     * 根据ID获取商品详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取商品详情", description = "根据商品ID获取商品详细信息")
    @ApiResponse(responseCode = "200", description = "获取成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Product.class)))
    public ApiResult<Product> getProductById(
            @Parameter(description = "商品ID", required = true)
            @PathVariable("id") Long id) {
        return productService.getProductById(id);
    }

    /**
     * 创建商品
     */
    @PostMapping
    @Operation(summary = "创建商品", description = "创建新的商品信息")
    @ApiResponse(responseCode = "201", description = "创建成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Product.class)))
    public ApiResult<Product> createProduct(
            @Parameter(description = "商品信息", required = true)
            @RequestBody Product product) {
        return productService.createProduct(product);
    }

    /**
     * 更新商品
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新商品", description = "更新指定商品的信息")
    @ApiResponse(responseCode = "200", description = "更新成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Product.class)))
    public ApiResult<Product> updateProduct(
            @Parameter(description = "商品ID", required = true)
            @PathVariable("id") Long id,
            @Parameter(description = "商品信息", required = true)
            @RequestBody Product product) {
        return productService.updateProduct(id, product);
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除商品", description = "删除指定商品")
    @ApiResponse(responseCode = "204", description = "删除成功")
    public ApiResult<Void> deleteProduct(
            @Parameter(description = "商品ID", required = true)
            @PathVariable Long id) {
        return productService.deleteProduct(id);
    }
}